package com.egghiro.j2se.game.sace.model.actor;

import com.egghiro.framework.j2se.game.core.view.canvas.SGraphics;
import com.egghiro.framework.j2se.util.IterateRemover;
import com.egghiro.framework.j2se.util.SIterator;
import com.egghiro.framework.j2se.util.SLinkedList;

public class FlyerComposite extends FlyerComponent {
    private SLinkedList<FlyerComponent> flyerList;
    /**
     * 如果当前对象不包含任何子对象，则自动清除当前对象
     */
    public FlyerComposite() {
        this(true);
    }
    
    /**
     * 
     * @param isAutoRemove
     */
    public FlyerComposite(boolean isAutoRemove){
        this.flyerList = new SLinkedList<FlyerComponent>();
        this.isAutoRemove = isAutoRemove;
    }

    // protected void setFlyerList(SLinkedList<FlyerComponent> flyerList) {
    // this.flyerList = flyerList;
    // }

    /**
     * 添加FlyerComponent到此FlyerComposite中
     */
    @Override
    public synchronized void add(FlyerComponent flyerComponent) {// 如果不设为synchronized，在paint的途中发生add就会出现异常
        this.flyerList.add(flyerComponent);
    }

    @Override
    public void act(IterateRemover itRemover) {
        this.act();
        if (isAutoRemove) {
            if (this.flyerList.isEmpty()) {
                itRemover.remove();
            }   
        }
    }

    @Override
    public void act() {
        SIterator<FlyerComponent> it = flyerList.iterator();
        while (it.hasNext()) {
            it.next().act(it.getRemover());
        }

    }

    @Override
    public synchronized void paint(SGraphics graphics) {
        for (FlyerComponent flyer : flyerList) {
            flyer.paint(graphics);
        }
    }
}
